package com.ysm.smfu.fragment;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.ysm.smfu.R;
import com.ysm.smfu.activity.All_LoginActivity;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.hyphenate.chat.EMClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/9/28.
 */
public class LoginPsdSetNextFragment extends MyBaseFragment {
    @BindView(R.id.et_activity_register_phonenumber)
    EditText etActivityRegisterPhonenumber;
    @BindView(R.id.et_activity_register_phonenumber_again)
    EditText etActivityRegisterPhonenumberAgain;

    private String code;
    private String phone;
    private SharedPreferences sp;
    private String loginPsd;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle arguments = getArguments();
        if (arguments != null) {
            code = arguments.getString(SpKey.CODE);
            phone = arguments.getString(SpKey.PHONE);
            loginPsd = arguments.getString("loginPsd");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.fragment_psd_login_set);
        ButterKnife.bind(this, view);
        getConfig();
        return view;
    }

    private void getConfig() {
        sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
    }


    @OnClick(R.id.fragment_zhifu_next)
    public void onClick(View view) {
        String fPsd = etActivityRegisterPhonenumber.getText().toString().trim();
        if (TextUtils.isEmpty(fPsd)) {
            ToastUtils.showToast("密码不能为空");
            return;
        }
        if (!fPsd.matches(SpKey.MIMAREGEX)) {
            ToastUtils.showToast("密码至少包含一位字母");
            return;
        }
        String sPsd = etActivityRegisterPhonenumberAgain.getText().toString().trim();
        if (!TextUtils.equals(fPsd, sPsd)) {
            ToastUtils.showToast("两次密码不一致");
            return;
        }
//            修改登录密码
        if (TextUtils.isEmpty(loginPsd)) {
            modifyLoginPsd(fPsd, sPsd);
        } else {
//        从登录页过来的找回密码
            modifyLoginPagePsd(fPsd, sPsd);
        }
    }

    /**
     * 从登录页过来的找回密码
     *
     * @param fPsd
     * @param sPsd
     */
    private void modifyLoginPagePsd(String fPsd, String sPsd) {
        BaseProtocal.builder(false).modifyLoginPagePsd(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                String state = JsonUtil.getState(response);
                if (!TextUtils.equals(state, "1")) {
                    ToastUtils.showToast(JsonUtil.getMessage(response));
                    return;
                }
                // 保存用户名账号
                sp.edit().putString(SpKey.NAME, phone).commit();
                showSuccessDialog();
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
            }
        }, fPsd, sPsd, phone, code);
    }


    /**
     * 修改登录密码
     *
     * @param fPsd
     * @param sPsd
     */
    private void modifyLoginPsd(String fPsd, String sPsd) {
        BaseProtocal.builder(true).modifyLoginPsd(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("修改登录密码",response);
                String state = JsonUtil.getState(response);
                if (!TextUtils.equals(state, "1")) {
                    ToastUtils.showToast(JsonUtil.getMessage(response));
                    return;
                }
                // 保存用户名账号
                sp.edit().putString(SpKey.NAME, phone).commit();
                showSuccessDialog();
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
            }
        }, fPsd, sPsd, phone, code);
    }

    /**
     * 找回成功对话框，并跳往登录页
     *
     * @param
     */
    public void showSuccessDialog() {

        //由重新设置密码页面跳转过来
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setMessage("修改成功").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                if (TextUtils.isEmpty(loginPsd))
//                    getActivity().finish();
//                else {
                EMClient.getInstance().logout(false);
                BaseActivity activity = (BaseActivity) getActivity();
                activity.killOthersElseMainActivty();
                UIUtils.startActivity(All_LoginActivity.class, "isModifyPsd", "true");
//                }
            }
        });
        final AlertDialog dialog = builder.create();
        dialog.show();
    }
}
